<?php
function get_select_sql($tablename, $select='', $where='', $orderby='', $limit='')
{
    $sql = "select ";
    if($select=='')
    {
        $sql .=" * ";
    }
    else
    {
        if(is_array($select))
        {
            for( $i=0; $i< count($select); $i++)
            {
                if($i == 0)
                {
                    $sql .= $select[$i];
                }
                else
                {
                    $sql .= ", ".$select[$i];
                }
            }
        }
        else
        {
            $sql .= $select;
        }
    }
    $sql .= " from $tablename ";
    if($where!='')
    {
        
        if(is_array($where))
        {
            $sql .= " where ";
            $wheres =array();
            foreach($where as $key=>$value)
            {
                array_push($wheres,"`$key`='$value'");
            }
            $sql .= implode(" and ", $wheres);
        }
        else
        {
            $sql .= " where ".$where;
        }
    }
    
    if( $orderby !='')
    { 
        $sql .= " order by $orderby ";
    }
    
    if($limit != '')
    {
        $sql .= " $limit ";
    }
    
    return $sql;
}

function unserializeToArray($value)
{
    if($value=="")
    {
        return array();
    }
    else if(!is_array(unserialize($value)))
    {
        return array();
    }
    else
    {
        return unserialize($value);
    }
}
function generateInsertSql($tableName, $array)
		{
				if($tableName=='' || $array==null || !is_array($array) || count($array)<=0)
				{
					return '';
				}
        $sql = "insert into $tableName(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "$key";
                $first=1;
            }
            else
            {
                $sql .= ",$key";
            }
        }
        $sql .=")values(";
        $first=0;
        foreach( $array as $key=>$value)
        {
            if($first==0)
            {
                $sql .= "'$value'";
                $first=1;
            }
            else
            {
                $sql .= ",'$value'";
            }
        }
        $sql .= ")";
        return $sql;
		}
?>
